home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / freeze-2.lha / freeze / showhuf.c < prev    next >
C/C++ Source or Header  |  1993-02-22  |  2KB  |  100 lines

  1. #include "freeze.h"
  2. #include "huf.h"
  3.  
  4. uc_t Table2[9];
  5.  
  6. /* prints out Huffman information from a frozen file, just for fun
  7.  * and testing purposes.
  8.  */
  9.  
  10. int main(argc, argv)
  11. int argc; char ** argv;
  12. {
  13.     if (argc != 2) {
  14.         fprintf(stderr, "Usage: showhuf frozen_file\n");
  15.         return 1;
  16.     }
  17.  
  18.     if (freopen(argv[1], "r", stdin) == NULL) {
  19.         fprintf(stderr, "showhuf: can't open file %s", argv[1]);
  20.         perror(" ");
  21.         return 1;
  22.     }
  23.     if (getchar() != MAGIC1)
  24.         goto reject;
  25.  
  26.     switch (getchar()) {
  27.     case MAGIC2_1:
  28.         printf("%s: no Huffman table (old-style)\n", argv[1]);
  29.         return 0;
  30.  
  31.     case MAGIC2_2:
  32.         break;
  33.  
  34.     default: reject:
  35.         printf("%s is not in frozen format\n", argv[1]);
  36.         return 0;
  37.     }
  38.  
  39.     /* Now the real work begins... */
  40.  
  41.     printf("%s: ", argv[1]);
  42.  
  43.     if (read_header() == EOF)
  44.         return 1;
  45.  
  46.     printf("%d,%d,%d,%d,%d,%d,%d,%d\n",
  47.         Table2[1], Table2[2], Table2[3], Table2[4],
  48.         Table2[5], Table2[6], Table2[7], Table2[8]);
  49.  
  50.     /* ... and ends */
  51.  
  52.     return 0;
  53. }
  54.  
  55. /* Reconstructs `Table' from the header of the frozen file and checks
  56.     its correctness. Returns 0 if OK, EOF otherwise.
  57. */
  58.  
  59. int read_header() {
  60.     short i, j;
  61.     i = getchar() & 0xFF;
  62.     i |= (getchar() & 0xFF) << 8;
  63.     Table2[1] = i & 1; i >>= 1;
  64.     Table2[2] = i & 3; i >>= 2;
  65.     Table2[3] = i & 7; i >>= 3;
  66.     Table2[4] = i & 0xF; i >>= 4;
  67.     Table2[5] = i & 0x1F; i >>= 5;
  68.  
  69.     if (i & 1 || (i = getchar()) & 0xC0) {
  70.         fprintf(stderr, "Unknown header format.\n");
  71.         return EOF;
  72.     }
  73.  
  74.     Table2[6] = i & 0x3F;
  75.  
  76.     i = Table2[1] + Table2[2] + Table2[3] + Table2[4] +
  77.     Table2[5] + Table2[6];
  78.  
  79.     i = 62 - i;     /* free variable length codes for 7 & 8 bits */
  80.  
  81.     j = 128 * Table2[1] + 64 * Table2[2] + 32 * Table2[3] +
  82.     16 * Table2[4] + 8 * Table2[5] + 4 * Table2[6];
  83.  
  84.     j = 256 - j;    /* free byte images for these codes */
  85.  
  86. /*      Equation:
  87.         Table[7] + Table[8] = i
  88.     2 * Table[7] + Table[8] = j
  89. */
  90.     j -= i;
  91.     if (j < 0 || i < j) {
  92.         printf("Warning - bad Huffman table");
  93.         return 0;
  94.     }
  95.     Table2[7] = j;
  96.     Table2[8] = i - j;
  97.  
  98.     return 0;
  99. }
  100.